/*
* rosa_delays.c
*
* Created: 2014-12-11 16:47:22
*  Author: klund
*/
#include "API/rosa.h"
#include "kernel/rosa_queues.h"
#include "kernel/rosa_task.h"
#include "kernel/rosa_scheduler.h"
#include "kernel/rosa_semaphore_internal.h"

/// Delays the currently executing task by ticksToDelay ticks
void ROSA_taskDelay(timerType ticksToDelay)
{
	tcb *currentTask = getCurrentTask();
	if(taskHasLockedIPCPsemaphore(currentTask))
		asm("breakpoint");		// You should never use taskDelay in a critical section
	currentTask->state = Waiting;
	currentTask->wakeUpTime = ROSA_getCurrentTick() + ticksToDelay;
	scheduler();
}

/// Delays the currently executing task until the current tick count equals (ticks + period)
void ROSA_taskDelayUntil(timerType previousTime, timerType ticksToDelay)
{
	tcb *currentTask = getCurrentTask();
	if(taskHasLockedIPCPsemaphore(currentTask))
		asm("breakpoint");		// You should never use taskDelay in a critical section	
	currentTask->state = Waiting;
	currentTask->wakeUpTime = previousTime + ticksToDelay;
	scheduler();
}